package sjtu.ist.warehouse4ws.tool.parser;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import sjtu.ist.warehouse4ws.model.Domainmodel;
import sjtu.ist.warehouse4ws.model.Functions;
import sjtu.ist.warehouse4ws.model.Process;
import sjtu.ist.warehouse4ws.tool.ParserProcess;

public class ProcessParser {
	private File file = null; // 模型文件
	private Document doc = null; // 文档对象
	private Process process;
	private String processId;
	private ArrayList<Functions> funList = null;

	public ProcessParser(File file) {
		this.file = file;
		process = new Process();
		
		if (file == null) {
			System.out.println("未提供模型文件");
			return;
		}
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		try {
			db = dbf.newDocumentBuilder();
			doc = db.parse(file);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void parse() {	
		resolveProcess();
		resolveFunctions();
	}

	public String parseNode(String tagName, Document doc) {

		NodeList parentlist = doc.getElementsByTagName(tagName); // 获取该ID的标签

		Element head = (Element) parentlist.item(0); // 获取该ID标签的第一个Item

		NodeList childlist = head.getChildNodes(); // 获取该ID标签第一个Item里面的结点
		if (childlist.getLength() != 0) {
			String tagvalue = childlist.item(0).getNodeValue(); // 取值
			if (tagvalue.isEmpty()) {
				return null;
			} else {
				return tagvalue;
			}
		} else {
			return null;
		}

	}

	/**
	 * 解析process
	 * 
	 * @param path
	 */
	public void resolveProcess() {

		processId = parseNode("id", doc);
		String enName = parseNode("enName", doc);
		String cnName = parseNode("cnName", doc);
		String description = parseNode("description", doc);

		process.setId(processId);
		process.setEnName(enName);
		process.setCnName(cnName);
		process.setDescription(description);
		// process.setFileinfo(null);
	}

	/**
	 * 解析function
	 */
	public void resolveFunctions() {

		funList = new ArrayList<Functions>();

		Element root = doc.getDocumentElement();
		NodeList functionlist = root.getElementsByTagName("string");
		for (int i = 0; i < functionlist.getLength(); i++) {
			Functions functions = new Functions();
			Element funId = (Element) functionlist.item(i);

			String funid = funId.getFirstChild().getNodeValue();
			// System.out.println(funid);
			functions.setFunctionid(funid);
			functions.setProcessid(processId);
			funList.add(functions);
		}
	}

	/*
	 * public String getNodeText(NodeList node){ Element
	 * e=(Element)node.item(0); if(e==null){return null;} //this judge is very
	 * important ,without it ,you will found nullpoint Exception Text
	 * t=(Text)e.getFirstChild(); if(t==null){return null;} String
	 * str=t.getNodeValue(); return str;
	 * 
	 * }
	 */

	public static void main(String[] args) throws IOException, Exception {

		ParserProcess test = new ParserProcess();
		String path = "E:/runtime-EclipseApplication/demo2/功能视图/processes/11c4b74b-874c-4f1d-add8-ad012165a1d5.xml";
		Process p = test.reserveProcess(path);
		System.out.println(p.getCnName());
		System.out.println(p.getId());
		ArrayList<Functions> testfun = test.reserveFunctions(path);
		for (int i = 0; i < testfun.size(); i++) {
			System.out.println(testfun.get(i).getFunctionid());
			System.out.println(testfun.get(i).getProcessid());

		}

	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public Document getDoc() {
		return doc;
	}

	public Process getProcess() {
		return process;
	}

	public String getProcessId() {
		return processId;
	}

	public ArrayList<Functions> getFunList() {
		return funList;
	}
}
